package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.Timer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Workspace extends JFrame {
	private JButton btnMachine, btnRepair, btnOrder, btnSearch, btnLogOff,
			btnSparePart, btnCreate, btnUpdate, btnRemove, btnUp;
	private JTextField txfSearch;
	private JLabel lblList;
	private JList listMain;
	private JScrollPane scpMain;
	private Controller controller = new Controller();
	private boolean state;
	private Timer timer;

	public Workspace() {
		this.setTitle("Inventory Manager");
		this.setSize(500, 300);
		this.setLocation(20, 40);
		this.setLayout(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		btnMachine = new JButton("Machine");
		this.add(btnMachine);
		btnMachine.setLocation(30, 50);
		btnMachine.setSize(100, 30);
		btnMachine.addActionListener(controller);

		btnRepair = new JButton("Repair");
		this.add(btnRepair);
		btnRepair.setLocation(30, 100);
		btnRepair.setSize(100, 30);
		btnRepair.addActionListener(controller);

		btnSparePart = new JButton("Spare Part");
		this.add(btnSparePart);
		btnSparePart.setLocation(30, 150);
		btnSparePart.setSize(100, 30);
		btnSparePart.addActionListener(controller);

		btnOrder = new JButton("Order");
		this.add(btnOrder);
		btnOrder.setLocation(30, 200);
		btnOrder.setSize(100, 30);
		btnOrder.addActionListener(controller);

		listMain = new JList();
		listMain.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listMain.addListSelectionListener(controller);

		scpMain = new JScrollPane(listMain);
		scpMain.setLocation(150, 50);
		scpMain.setSize(100, 200);
		this.add(scpMain);

		lblList = new JLabel("Show List");
		this.add(lblList);
		lblList.setSize(100, 20);
		lblList.setLocation(170, 30);

		btnSearch = new JButton("Search");
		btnSearch.setSize(100, 20);
		btnSearch.setLocation(300, 10);
		btnSearch.addActionListener(controller);
		this.add(btnSearch);

		btnLogOff = new JButton("Logoff");
		btnLogOff.setSize(80, 20);
		btnLogOff.setLocation(410, 10);
		btnLogOff.addActionListener(controller);
		this.add(btnLogOff);

		txfSearch = new JTextField();
		this.add(txfSearch);
		txfSearch.setLocation(100, 10);
		txfSearch.setSize(200, 20);

		//--------------------------------------------------------------
		timer = new Timer(50, controller);

		btnCreate = new JButton("Create");
		btnCreate.setSize(100, 20);
		btnCreate.setLocation(50, 330);
		btnCreate.addActionListener(controller);
		this.add(btnCreate);

		btnRemove = new JButton("Remove");
		btnRemove.setSize(100, 20);
		btnRemove.setLocation(180, 330);
		btnRemove.addActionListener(controller);
		this.add(btnRemove);

		btnUpdate = new JButton("Update");
		btnUpdate.setSize(100, 20);
		btnUpdate.setLocation(300, 330);
		btnUpdate.addActionListener(controller);
		btnUpdate.setFocusable(true);
		this.add(btnUpdate);

		btnUp = new JButton("\u25B2");
		btnUp.setSize(50, 20);
		btnUp.setLocation(430, 290);
		btnUp.addActionListener(controller);
		btnUp.setFocusable(false);
		this.add(btnUp);
	}

	public class Controller implements ActionListener, ListSelectionListener {
		private int resize = 20;
		private int open = 0;

		public void actionPerformed(ActionEvent e) {

			if (e.getSource() == btnMachine || e.getSource() == btnUp) {
				open = 1;
				timer.start();
			}

			if (e.getSource() == btnUp) {
				open = 2;
				timer.start();
			}

			int width = Workspace.this.getWidth();
			int height = Workspace.this.getHeight();

			if (open == 1 && height < 400) {
				Workspace.this.setSize(width, height + resize);
			}

			if (open == 2 && height > 300) {
				Workspace.this.setSize(width, height - resize);
			}

			if (e.getSource() == btnLogOff) {
				state = false;
				LogON log = new LogON();
				setVisible(false);
				log.setVisible(true);
			}
		}

		public void valueChanged(ListSelectionEvent arg0) {

		}
	}

	public boolean isState() {
		return state;
	}
	public static void main(String[] args) {
		Workspace f = new Workspace();
		f.setVisible(true);
	}

}
